﻿#pragma once
#include "ThostFtdcMdApi.h"
#include <functional>
#include "MarketDataManager.h"

class CMdUserSpi : public CThostFtdcMdSpi
{
public:
	CMdUserSpi(MarketDataManager &marketManager);
	virtual ~CMdUserSpi();
	// 设置前置连接返回成功后的回调，可以在这进行登录操作了
	void SetFrontConnectedFunc(std::function<void(void)> pFunc);
	// 设置登录后的回调
	void SetLoginedFunc(std::function<void(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)> pFunc);
	//// 设置取消订阅的回调
	//void SetUnSubscribeFunc(std::function<void(const std::list<std::string> &pList)> pFunc);
	// 返回要监控的合约列表
	//void GetVariableSymbolList(std::list<std::string> &outList);

protected:
	virtual void OnFrontConnected() override;
	virtual void OnFrontDisconnected(int nReason) override;
	virtual void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRspError(CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;

	virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField* pDepthMarketData) override;

	virtual void OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
	virtual void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField* pForQuoteRsp) override;

private:
	//// 得到合约系列名称
	//std::string getClassName(TThostFtdcInstrumentIDType pSymbol);
	//// 保存今日的主力合约
	//void saveMainSymbol();
	//// 初始化上次的主力合约
	//void initTheLastMainSymbol();

	// 前置连接后的回调
	std::function<void(void)> m_frontConnectedFunc;
	// 登录成功后的回调
	std::function<void(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)> m_loginedFunc;
	// 取消行情订阅的回调
	//std::function<void(const std::list<std::string> &pList)> m_unSubscribeFunc;

	MarketDataManager &m_marketManager;

};

